<script setup lang="ts">
import { Form } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { PropType, reactive, watch, ref } from 'vue'
import { ProductData } from '@/api/product/types'
import { useI18n } from '@/hooks/web/useI18n'
import { useValidator } from '@/hooks/web/useValidator'
import { FormSchema } from '@/types/form'

const { required } = useValidator()

const props = defineProps({
  currentRow: {
    type: Object as PropType<Nullable<ProductData>>,
    default: () => null
  }
})

const { t } = useI18n()

const schema = reactive<FormSchema[]>([
  {
    field: 'categoryId',
    label: '商品分类',
    component: 'Select',
    componentProps: {
      options: [
        {
          label: 'java基础-书籍',
          value: '1'
        }
      ]
    }
  },
  {
    field: 'createUser',
    label: '创建人',
    component: 'Select',
    componentProps: {
      options: [
        {
          label: '思云后台管理人员',
          value: '1'
        }
      ]
    }
  },
  {
    field: 'productName',
    label: '商品名称',
    component: 'Input'
  },
  {
    field: 'mainImage',
    label: '商品图片',
    component: 'Input'
  },
  {
    field: 'productDescription',
    label: '商品介绍',
    component: 'Input'
  },
  {
    field: 'ProductLabel',
    label: '商品标签',
    component: 'Input'
  },
  {
    field: 'productCode',
    label: '商品货号',
    component: 'Input'
  },
  {
    field: 'currentpriceProduct',
    label: '商品售价',
    component: 'Input'
  },
  {
    field: 'searchKeywords',
    label: '关键字',
    component: 'Input'
  },
  {
    field: 'state',
    label: '审核状态',
    component: 'Select',
    componentProps: {
      options: [
        {
          label: '审核中',
          value: '2'
        },
        {
          label: '审核通过',
          value: '3'
        },
        {
          label: '审核不通过',
          value: '4'
        }
      ]
    }
  },
  {
    field: 'recommend',
    label: '商品推荐',
    component: 'Radio',
    componentProps: {
      options: [
        {
          label: '新品',
          value: '2'
        },
        {
          label: '推荐',
          value: '3'
        },
      ]
    }
  },
  {
    field: 'discount',
    label: '可用优惠券',
    component: 'Radio',
    componentProps: {
      options: [
        {
          label: '是',
          value: '2'
        },
        {
          label: '否',
          value: '3'
        },
      ]
    }
  },
  {
    field: 'ensure',
    label: '服务保证',
    component: 'Radio',
    componentProps: {
      options: [
        {
          label: '无忧退货',
          value: '2'
        },
        {
          label: '快速退货',
          value: '3'
        },
        {
          label: '免费包邮',
          value: '4'
        }
      ]
    }
  },

])


const { register, methods, elFormRef } = useForm({
  schema
})

watch(
  () => props.currentRow,
  (currentRow) => {
    if (!currentRow) return
    const { setValues, setSchema } = methods
    setValues(currentRow)
    setSchema([
      {
        field: 'remark',
        path: 'componentProps.defaultHtml',
        value: currentRow
      }
    ])
  },
  {
    deep: true,
    immediate: true
  }
)

defineExpose({
  elFormRef,
  getFormData: methods.getFormData
})
</script>

<template>
  <Form :rules="rules" @register="register">
  </Form>
</template>
